from openai import OpenAI

client = OpenAI(
    api_key="sk-7b4cb6aa21fa442f90bbe851265dd008",
    base_url="https://api.deepseek.com"          # 去掉末尾空格
)

message = [{"role": "user", "content": "说一个程序员才懂的笑话"}]

# 开启流式返回
completion = client.chat.completions.create(
    model="deepseek-chat",
    messages=message,
    stream=True           # ← 关键
)

try:
    for chunk in completion:
        if chunk.choices and chunk.choices[0].delta.content is not None:
            print(chunk.choices[0].delta.content, end='')
except Exception as e:
    print("\n[Error]", e)